package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.Orders;
import cn.edu.ujn.lab3.dao.Products;
import cn.edu.ujn.lab3.service.CustomerService;
import cn.edu.ujn.lab3.service.OrdersService;
import cn.edu.ujn.lab3.service.ProductsService;
import cn.edu.ujn.lab3.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    
    @Autowired
    private CustomerService customerService;

    @Autowired
    private ProductsService productsService;
    //查找订单列表
    @RequestMapping(value = "/list.action")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer rows,
                       Orders orders, Model model) {

        orders.setRows(rows);
        Page<Orders> ordersList = ordersService.findOrdersList(page, orders);
        model.addAttribute("page", ordersList);
        model.addAttribute("prodName", orders.getProdName());
        model.addAttribute("custName", orders.getCustName());
        model.addAttribute("shopState", orders.getShopState());
        return "orders";
    }
    
    //创建订单
    @RequestMapping("/create.action")
    @ResponseBody
    public String ordersCreate(Orders orders) {
        String custName = orders.getCustName();
        String prodName = orders.getProdName();
        Customer customerByName = customerService.getCustomerByName(custName);
        Products productsByName = productsService.getProductsByName(prodName);

        if (customerByName == null|| productsByName==null) {
            return "err";
        }
        orders.setCustId(customerByName.getCust_id().toString());
        orders.setProdId(productsByName.getId().toString());
        int rows = ordersService.createOrders(orders);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //根据id查询订单
    @RequestMapping("/getOrdersById.action")
    @ResponseBody
    public Orders getOrdersById(Integer id) {
        return ordersService.getOrdersById(id);
    }
    
    //更新订单
    @RequestMapping("/update.action")
    @ResponseBody
    public String ordersUpdate(Orders orders) {
        String custName = orders.getCustName();
        String prodName = orders.getProdName();
        Customer customerByName = customerService.getCustomerByName(custName);
        Products productsByName = productsService.getProductsByName(prodName);

        if (customerByName == null|| productsByName==null) {
            return "err";
        }
        orders.setCustId(customerByName.getCust_id().toString());
        orders.setProdId(productsByName.getId().toString());
        int rows = ordersService.updateOrders(orders);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }
    
    //删除订单
    @RequestMapping("/delete.action")
    @ResponseBody
    public String ordersDelete(Integer id){
        int rows = ordersService.deleteOrders(id);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

}